package com.leetcode.array_matrix;

/**
 * @author Dennis Li
 * @date 2020/7/22 21:49
 */
public class SearchMatrix_240 {

    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
        int row = 0, col = matrix[0].length - 1;
        // 从右上角看类似一个BST
        while (row < matrix.length && col >= 0) {
            if (matrix[row][col] == target)
                return true;
            else if (matrix[row][col] > target)
                col--;
            else
                row++;
        }
        return false;
    }
}
